﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BrightIdeasSoftware;
using MovieBrowser.Controller;
using MovieBrowser.Model;
using MovieBrowser.Util;

namespace MovieBrowser.Presentation.List.SearchDialogs
{
    public partial class SearchActorsForm : Form
    {
        private readonly MovieBrowserController _controller;

        public SearchActorsForm(MovieBrowserController controller)
        {
            _controller = controller;
            InitializeComponent();
            //listKeywords.DataSource = _controller.Db.Actors.Select(o => o.Person).ToList().Distinct();
            var _actors = _controller.Db.Actors.Select(o => o.Person).ToList();
            var _person = _controller.Db.People.ToList();
            _person = _actors.Intersect(_person).ToList();
            listKeywords.DataSource = _person;
        }


        private void ClSearchClick(object sender, EventArgs e)
        {
            //var movies = _controller.Db.Movies.Select(o => o);

            if (chkKeywords.Checked)
            {
                var list = (from Person person in listKeywords.CheckedObjects select person.Id).ToList();
                SearchResult = list;
                //var t = _controller.Db.MovieKeywords.WhereIn(o => o.Keyword.Id, list).Select(o => o.Movie);
                //movies = movies.Intersect(t);
            }
            else
            {
                SearchResult = new List<long>();
            }
            Hide();
        }

        public List<long> SearchResult { get; set; }

        private void SearchKeywordsSearchStarted(object sender, EventArgs e)
        {
            ComponentUtility.TimedFilter(listKeywords, searchKeywords.Text);
        }

        private void SearchKeywordsSearchCancelled(object sender, EventArgs e)
        {
            ComponentUtility.TimedFilter(listKeywords, "");
        }

        private void ChkClearCheckedChanged(object sender, EventArgs e)
        {
            OnCheckClearClick(listKeywords, chkClear);
        }

        private static void OnCheckClearClick(DataListView dataListView, CheckBox chk)
        {
            if (dataListView.SelectedItems.Count == 0)
            {
                foreach (OLVListItem selectedObject in dataListView.Items)
                {
                    selectedObject.Checked = chk.Checked;
                }
            }
            else
            {
                foreach (OLVListItem selectedObject in dataListView.SelectedItems)
                {
                    selectedObject.Checked = chk.Checked;
                }
            }
        }

        private void ChkKeywordsCheckedChanged(object sender, EventArgs e)
        {
            listKeywords.Enabled = chkKeywords.Checked;
        }

        private void ClCancelClick(object sender, EventArgs e)
        {
            Hide();
        }

        private void ClResetClick(object sender, EventArgs e)
        {
            listKeywords.DataSource = _controller.Db.Actors;
            chkKeywords.Checked = false;
        }
    }
}
